package org.csu.cms.controller;

import jakarta.validation.constraints.NotNull;
import org.csu.cms.domain.Category;
import org.csu.cms.domain.Item;
import org.csu.cms.domain.Product;
import org.csu.cms.result.Code;
import org.csu.cms.result.Result;
import org.csu.cms.service.CatalogService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@MapperScan("org.csu.cms.dao")
public class CatalogController {
    @Autowired
    private CatalogService catalogService;

    // 测试
//    @GetMapping("/")
//    public String test(){
//        System.out.println("hello");
//        return "hello world";
//    }

    // Category
    // eg: https://localhost:8080/category
    @GetMapping("/category")
    public Result getCategoryList(){
        return catalogService.getCategoryList();
    }

    // eg: https://localhost:8080/category/BIRDS
    @GetMapping("/category/{categoryId}")
    public Result getCategoryById(@PathVariable @NotNull String categoryId){
        return catalogService.getCategoryById(categoryId);
    }

    // eg: https://localhost:8080/category
    /*
    {
        "categoryId":"CATEGORY",
        "name":"Category",
        "description":"category"
    }
     */
    @PostMapping("/category")
    public Result insertCategory(@RequestBody @NotNull Category category){
        return catalogService.insertCategory(category);
    }

    // eg: https://localhost:8080/category
    /*
    {
        "categoryId":"CATEGORY",
        "name":"Category",
        "description":"category"
    }
     */
    @PutMapping("/category")
    public Result updateCategory(@RequestBody @NotNull Category category){
        return catalogService.updateCategory(category);
    }

    // eg: https://localhost:8080/category/CATEGORY
    @DeleteMapping("/category/{categoryId}")
    public Result deleteCategory(@PathVariable @NotNull String categoryId){
        return catalogService.deleteCategory(categoryId);
    }


    // Product
    // eg: https://localhost:8080/product
    @GetMapping("/product")
    public Result getProduct(){
        return catalogService.getProductList();
    }

    // eg: https://localhost:8080/product/Goldfish
    @GetMapping("/product/{productId}")
    public Result getProductById(@PathVariable String productId){
        return catalogService.getProductById(productId);
    }

    // eg: https://localhost:8080/product/category?categoryId=BIRDS
    @GetMapping("/product/category")
    public Result getProductByCategoryId(@RequestParam String categoryId){
        return catalogService.getProductByCategoryId(categoryId);
    }

    // eg: https://localhost:8080/product
    /*
    {
        "productId":"PRODUCT",
        "categoryId":"CATEGORY",
        "name":"Product",
        "description":"product",
        "thum":null
    }
     */
    @PostMapping("/product")
    public Result insertProduct(@RequestBody @NotNull Product product){
        return catalogService.insertProduct(product);
    }

    // eg: https://localhost:8080/product
    /*
    {
        "productId":"PRODUCT",
        "categoryId":"CATEGORY",
        "name":"Product",
        "description":"product",
        "thum":null
    }
     */
    @PutMapping("/product")
    public Result updateProduct(@RequestBody @NotNull Product product){
        return catalogService.updateProduct(product);
    }

    // eg: https://localhost:8080/product/PRODUCT
    @DeleteMapping("/product/{productId}")
    public Result deleteProduct(@PathVariable String productId){
        return catalogService.deleteProduct(productId);
    }


    // Item
    // eg: https://localhost:8080/item
    @GetMapping("/item")
    public Result getItemList(){
        return catalogService.getItemList();
    }

    // eg: https://localhost:8080/item/EST-1
    @GetMapping("/item/{itemId}")
    public Result getItemListById(@PathVariable @NotNull String itemId){
        return catalogService.getItemById(itemId);
    }

    // eg: https://localhost:8080/item/category?categoryId=BIRDS
    @GetMapping("/item/category")
    public Result getItemListByCategoryId(@RequestParam @NotNull String categoryId){
        return catalogService.getItemByCategoryId(categoryId);
    }

    // eg: https://localhost:8080/item/product?productId=Goldfish
    @GetMapping("/item/product")
    public Result getItemListByProductId(@RequestParam @NotNull String productId){
        return catalogService.getItemByProductId(productId);
    }

    // eg: https://localhost:8080/item
    /*
    {
        "itemId":"ITEM",
        "productId":"PRODUCT",
        "listPrice":"12.32",
        "unitCost":"12.32",
        "supplier":1,
        "status":'P',
        "attribute1":"ATTR1",
        "attribute2":null,
        "attribute3":null,
        "attribute4":null,
        "attribute5":null,
        "quantity":10000
    }
     */
    @PostMapping("/item")
    public Result insetItem(@RequestBody @NotNull Item item){
        return catalogService.insertItem(item);
    }

    // eg: https://localhost:8080/item
    /*
    {
        "itemId":"ITEM",
        "productId":"PRODUCT",
        "listPrice":"12.32",
        "unitCost":"12.32",
        "supplier":1,
        "status":'P',
        "attribute1":"ATTR1",
        "attribute2":null,
        "attribute3":null,
        "attribute4":null,
        "attribute5":null,
        "quantity":10000
    }
     */
    @PutMapping("/item")
    public Result updateItem(@RequestBody @NotNull Item item){
        return catalogService.updateItem(item);
    }

    // eg: https://localhost:8080/item/ITEM
    @DeleteMapping("/item/{itemId}")
    public Result deleteItem(@PathVariable @NotNull String itemId){
        return catalogService.deleteItem(itemId);
    }



    // 搜索
    // eg: https://localhost:8080/search?classname=category&keyword=KEY
    @GetMapping("/search")
    public Result search(@RequestParam @NotNull String classname, @RequestParam @NotNull String keyword){
        switch (classname){
            case "category" -> {
                return catalogService.categorySearch(keyword);
            }
            case "product" -> {
                return catalogService.productSearch(keyword);
            }
            default -> {
                return new Result(Code.GET_ERR, "查询失败，类名参数错误");
            }
        }
    }



}
